﻿using EFFC.Frame.Net.Base.Common;
using EFFC.Frame.Net.Base.Data.Base;
using EFFC.Frame.Net.Global;
using EFFC.Frame.Net.Module.HttpCall;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace XHRPMIS.Common
{
    public class APIAccess : SimpleRestCallHelper
    {
        public APIAccess(string access_id = "", string access_token = "")
        {
            Access_ID = string.IsNullOrEmpty(access_id) ? MyConfig.GetConfiguration("APIManage", "Access_ID") : access_id;
            AccessToken = string.IsNullOrEmpty(access_token) ? MyConfig.GetConfiguration("APIManage", "Access_Token") : access_token;
        }
        /// <summary>
        /// 访问远程API的系统编号
        /// </summary>
        public object Access_ID { get; private set; }
        /// <summary>
        /// 访问远程API的Token
        /// </summary>
        public object AccessToken { get; private set; }
        protected string Send(string url, object data, string method)
        {
            FrameDLRObject content = FrameDLRObject.CreateInstance(data, EFFC.Frame.Net.Base.Constants.FrameDLRFlags.SensitiveCase);
            var result = base.Send(url, data, new
            {
                Authorization = $"Bearer {AccessToken}",
                Sub_System_id = Access_ID
            }, method);
            GlobalCommon.Logger.WriteLog(EFFC.Frame.Net.Base.Constants.LoggerLevel.DEBUG, $"APIAccess：\nRequest url:{method} {url};Response Result={result}");
            if (result.StartsWith("Failed:")) result = "";
            return result;
        }
        protected string Get(string url)
        {
            var result = base.Get(url, new
            {
                Authorization = $"Bearer {AccessToken}",
                Sub_System_id = Access_ID
            });
            GlobalCommon.Logger.WriteLog(EFFC.Frame.Net.Base.Constants.LoggerLevel.DEBUG, $"APIAccess：\nRequest url:Get {url};Response Result={result}");
            if (result.StartsWith("Failed:")) result = "";
            return result;
        }
        /// <summary>
        /// 获取远程API的基本信息描述
        /// </summary>
        /// <param name="server_url"></param>
        /// <returns></returns>
        public string GetAPIInfo(string server_url)
        {
            return Get(server_url + "/api/info");
        }
        /// <summary>
        /// 启用远程API的对外服务
        /// </summary>
        /// <param name="server_url"></param>
        /// <returns></returns>
        public string SetAPIOn(string server_url)
        {
            return Send(server_url + "/api/on", null, "patch");
        }
        /// <summary>
        /// 关闭远程API的对外服务
        /// </summary>
        /// <param name="server_url"></param>
        /// <returns></returns>
        public string SetAPIOff(string server_url)
        {
            return Send(server_url + "/api/off", null, "patch");
        }
        /// <summary>
        /// 获取API的log记录
        /// </summary>
        /// <param name="server_url"></param>
        /// <param name="start_time"></param>
        /// <param name="end_time"></param>
        /// <returns></returns>
        public string GetAPILog(string server_url,string start_time,string end_time)
        {
            return Get($"{server_url}/api/log?start_time={start_time}&end_time={end_time}");
        }
    }
}
